package com._520it.crm.web.controller;

import com._520it.crm.domain.OrderBill;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.OrderBillQueryObject;
import com._520it.crm.service.IOrderBillService;
import com._520it.crm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 呆 on 2017/10/3.
 */
@Controller
@RequestMapping("/orderBill")
public class OrderBillController {
    @Autowired
    private IOrderBillService orderBillService;
    @RequestMapping("")
    public String index(){
        return "orderBill";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(OrderBillQueryObject qo){
        PageResult pageResult = orderBillService.query(qo);
        return pageResult;
    }

    /**
     * 暂存单修改页面 传过来一个保单的id 然后查出相关所有信息
     * @param id 保单id
     * @return
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public OrderBill selectOne(Long id){
        OrderBill orderBill = orderBillService.selectByPrimaryKey(id);
        return orderBill;
    }
    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(OrderBill orderBill){
        Long id = orderBillService.insert(orderBill);
        return new AjaxResult(id+"");
    }
    //跳转至表单明细页面 进行表单编辑
    @RequestMapping("/orderBillItem")
    public String orderBillitem(Long orgId, Model model){
        model.addAttribute("orgId",orgId);
        return "orderBillItem";
    }
    //跳转至表单修改编辑页面
    @RequestMapping("/orderBillEdit")
    public String orderBillEdit(Long orgId, Model model){
        model.addAttribute("orgId",orgId);
        return "orderBillEdit";
    }
    //跳转至已提交为接受的表单信息界面
    @RequestMapping("/orderBillApply")
    public String orderBillApply(){
        return "orderbillapply";
    }
    //跳转至待修改表单信息界面
    @RequestMapping("/orderBillModify")
    public String orderBillModify(){
        return "orderbillmodify";
    }
    //跳转至所有表单信息界面
    @RequestMapping("/orderBillAll")
    public String orderBillAll(){
        return "orderbillall";
    }
    /*@RequestMapping("/searchcllientinfo")
    public String client(){
        return "searchcllientinfo";
    }*/
    //访问暂存单页面
    @RequestMapping("/orderbilllist")
    public String orderBillList(){
        return "orderbilllist";
    }


    /**
     * 在保单信息填写完成后 保存保单的信息
     * @param orderBill 保单对象 包含了 保单申请的所有信息
     * @return
     */
    @RequestMapping("updateTimeAndBaseInfo")
    @ResponseBody
    public AjaxResult updateTimeAndBaseInfo(OrderBill orderBill){
        AjaxResult result = null;
        try {
            orderBillService.updateTimeandBaseInfo(orderBill);
            result = new AjaxResult("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false,"保存失败");
        }
        return result;
    }

    /**
     * 表单申请批改的方法
     * @param orderId 表单id
     * @return
     */
    @RequestMapping("checkApply")
    @ResponseBody
    public AjaxResult checkApply(Long orderId ,String remark){
        AjaxResult result = null;
        try {
            orderBillService.checkApply(orderId,remark);
            result = new AjaxResult("申请成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false,"申请失败");
        }
        return result;
    }
    @RequestMapping("/updateApplyClient")
    @ResponseBody
    public AjaxResult updateApplyClient(OrderBill orderBill){
        AjaxResult result = null;
        try {
            /*orderBillService.updateApplyClient(orderBill);*/
            result = new AjaxResult("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false,"保存失败");
        }
        return result;
    }
    @RequestMapping("/updateTimeInfo")
    @ResponseBody
    public AjaxResult updateTimeInfo(OrderBill orderBill){
        AjaxResult result = null;
        try {
            /*orderBillService.updateTimeInfo(orderBill);*/
            result = new AjaxResult("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false,"保存失败");
        }
        return result;
    }
}
